package com.jxzyk.model.notes;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.core.entity.params.MapParams;
import com.core.entity.params.ReqParams;
import com.core.ui.activity.BaseActivity;
import com.core.util.T;
import com.core.util.UiTools;
import com.jxzyk.R;
import com.jxzyk.config.API;
import com.jxzyk.entity.Note;
import com.jxzyk.http.BaseRequest.OnDialogListener;
import com.jxzyk.http.req.NotesRequest;
import com.jxzyk.view.TitleLayout;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.act_note_details)
public class NoteDetailsActivity extends BaseActivity {
	@ViewInject(R.id.title)
	TitleLayout title;
	@ViewInject(R.id.tv_note_details_title)
	TextView tv_title;
	@ViewInject(R.id.tv_note_details_content)
	TextView tv_content;
	@ViewInject(R.id.tv_note_details_goods)
	TextView tv_goods;
	private Note note;

	@Override
	public void initData(Bundle bundle) {
		bundle = getIntent().getExtras();
		note = (Note) bundle.getSerializable(Note.class.getName());
	}

	@Override
	public void initUi() {
		title.setTtile("学习笔记");
		if (note != null) {
			tv_title.setText(note.getOutline_name());
			tv_content.setText(note.getNote_content());
			tv_goods.setText(note.getNote_good());
			tv_goods.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					sendGood();
				}
			});
		}
	}

	protected void sendGood() {
		NotesRequest request = new NotesRequest(NoteDetailsActivity.this,
				API.Notes.praise);
		ReqParams params = new MapParams();
		params.put("note_id", note.getNote_id());
		request.setParams(params);
		request.setOnDialogListener(new OnDialogListener() {

			@Override
			public void onSuccess(JSONObject json) {
				String message = json.getString("message");
				if (UiTools.Empty(message)) {
					T.toast(message);
				}
				int goods = Integer.parseInt(note.getNote_good()) + 1;
				tv_goods.setText(goods + "");
			}

			@Override
			public void onFailure(String msg) {

			}
		});
		request.show();
	}
}
